﻿//
// (C) Copyright 2009 Irantha Suwandarathna (iranthas@hotmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EffiProz.Core.Lib;
using EffiProz.Core.Rights;
using EffiProz.Core.Indexes;
using EffiProz.Core.Errors;
using EffiProz.Core.Results;
using EffiProz.Core.Navigators;
using EffiProz.Core.DataTypes;
using EffiProz.Core.Store;
using EffiProz.Core.Persist;

namespace EffiProz.Core
{
    public class StatementSchemaDefinition : StatementSchema
    {
        public StatementSchema[] statements;

        public StatementSchemaDefinition(StatementSchema[] statements)
            : base()
        {

            this.statements = statements;
        }

        public override Result execute(Session session)
        {

            Result result;

            try
            {
                result = getResult(session);
            }
            catch (Exception t)
            {
                result = Result.newErrorResult(t, null);
            }

            if (result.isError())
            {
                result.getException().setStatementType(group, type);
            }

            return result;
        }

        public Result getResult(Session session)
        {

            schemaName = statements[0].getSchemalName();

            if (this.isExplain)
            {
                return Result.newSingleColumnStringResult("OPERATION",
                        describe(session));
            }

            StatementSchema cs;
            Result result = statements[0].execute(session);
            EfzArrayList constraints = new EfzArrayList();

            if (statements.Length == 1 || result.isError())
            {
                return result;
            }

            QNameManager.QName oldSessionSchema = session.getCurrentSchemaHsqlName();

            for (int i = 1; i < statements.Length; i++)
            {
                try
                {
                    session.setSchema(schemaName.name);
                }
                catch (CoreException ) { }

                statements[i].setSchemaQName(schemaName);
                session.parser.reset(statements[i].getSQL());

                try
                {
                    session.parser.read();

                    switch (statements[i].getType())
                    {

                        case StatementTypes.GRANT:
                        case StatementTypes.GRANT_ROLE:
                            result = statements[i].execute(session);
                            break;

                        case StatementTypes.CREATE_TABLE:
                            cs = session.parser.compileCreate();
                            cs.isSchemaDefinition = true;

                            cs.setSchemaQName(schemaName);

                            if (session.parser.token.tokenType
                                    != Tokens.X_ENDPARSE)
                            {
                                throw session.parser.unexpectedToken();
                            }

                            result = cs.execute(session);

                            constraints.addAll((EfzArrayList)cs.arguments[1]);
                            ((EfzArrayList)cs.arguments[1]).clear();
                            break;

                        case StatementTypes.CREATE_ROLE:
                        case StatementTypes.CREATE_SEQUENCE:
                        case StatementTypes.CREATE_TYPE:
                        case StatementTypes.CREATE_CHARACTER_SET:
                        case StatementTypes.CREATE_COLLATION:
                            result = statements[i].execute(session);
                            break;

                        case StatementTypes.CREATE_INDEX:
                        case StatementTypes.CREATE_TRIGGER:
                        case StatementTypes.CREATE_VIEW:
                        case StatementTypes.CREATE_DOMAIN:
                        case StatementTypes.CREATE_ROUTINE:
                            cs = session.parser.compileCreate();
                            cs.isSchemaDefinition = true;

                            cs.setSchemaQName(schemaName);

                            if (session.parser.token.tokenType
                                    != Tokens.X_ENDPARSE)
                            {
                                throw session.parser.unexpectedToken();
                            }

                            result = cs.execute(session);
                            break;

                        case StatementTypes.CREATE_ASSERTION:
                        case StatementTypes.CREATE_TRANSFORM:
                        case StatementTypes.CREATE_TRANSLATION:
                        case StatementTypes.CREATE_CAST:
                        case StatementTypes.CREATE_ORDERING:
                            throw session.parser.unsupportedFeature();
                        default:
                            throw Error.runtimeError(ErrorCode.U_S0500, "");
                    }

                    if (result.isError())
                    {
                        break;
                    }
                }
                catch (CoreException e)
                {
                    result = Result.newErrorResult(e, statements[i].getSQL());
                }
            }

            if (!result.isError())
            {
                try
                {
                    for (int i = 0; i < constraints.size(); i++)
                    {
                        Constraint c = (Constraint)constraints.get(i);
                        Table table =
                            session.database.schemaManager.getUserTable(session,
                                c.core.refTableName);

                        ParserDDL.addForeignKey(session, table, c, null);
                    }
                }
                catch (CoreException e)
                {
                    result = Result.newErrorResult(e, sql);
                }
            }

            if (result.isError())
            {
                try
                {
                    session.database.schemaManager.dropSchema(schemaName.name,
                            true);
                    session.database.logger.writeToLog(
                        session, getDropSchemaStatement(schemaName));
                }
                catch (CoreException ) { }
            }

            try
            {
                session.setCurrentSchemaQName(oldSessionSchema);
            }
            catch (Exception ) { }

            return result;
        }

        /*
            if (constraintList != null && constraintList.size() > 0) {
                try {
                    for (int i = 0; i < constraintList.size(); i++) {
                        Constraint c = (Constraint) constraintList.get(i);
                        Table table = database.schemaManager.getUserTable(session,
                            c.core.refTableName);

                        addForeignKey(table, c);
                    }
                } finally {
                    constraintList.clear();
                }
            }
        */
        public String getDropSchemaStatement(QNameManager.QName schema)
        {
            return "DROP SCHEMA " + schema.statementName + " " + Tokens.T_CASCADE;
        }
    }
}
